package cn.sailing.plugins.position.amap;

import android.content.Context;
import android.util.Log;

import com.amap.api.track.AMapTrackClient;
import com.amap.api.track.ErrorCode;
import com.amap.api.track.OnTrackLifecycleListener;
import com.amap.api.track.TrackParam;
import com.amap.api.track.query.model.AddTerminalRequest;
import com.amap.api.track.query.model.AddTerminalResponse;
import com.amap.api.track.query.model.AddTrackResponse;
import com.amap.api.track.query.model.DistanceResponse;
import com.amap.api.track.query.model.HistoryTrackResponse;
import com.amap.api.track.query.model.LatestPointResponse;
import com.amap.api.track.query.model.OnTrackListener;
import com.amap.api.track.query.model.ParamErrorResponse;
import com.amap.api.track.query.model.QueryTerminalRequest;
import com.amap.api.track.query.model.QueryTerminalResponse;
import com.amap.api.track.query.model.QueryTrackResponse;

import java.util.Objects;

import cn.sailing.plugins.position.ITrackListener;
import cn.sailing.plugins.position.Utils;

public class AMapTrackService implements OnTrackListener {
    private final Context context;
    private final ITrackListener listener;
    private AMapTrackClient client;
    private final int SERVICE_ID = 301678;

    private OnTrackLifecycleListener callback = new OnTrackLifecycleListener() {
        @Override
        public void onBindServiceCallback(int i, String s) {

        }

        @Override
        public void onStartGatherCallback(int status, String msg) {
            if (status != ErrorCode.TrackListen.START_GATHER_SUCEE &&
                    status != ErrorCode.TrackListen.START_GATHER_ALREADY_STARTED) {
                Log.e("高德轨迹服务--采集回调", msg);
            }
        }

        @Override
        public void onStartTrackCallback(int status, String msg) {
            if (status == ErrorCode.TrackListen.START_TRACK_SUCEE ||
                    status == ErrorCode.TrackListen.START_TRACK_SUCEE_NO_NETWORK ||
                    status == ErrorCode.TrackListen.START_TRACK_ALREADY_STARTED) {
                client.startGather(callback);
            } else {
                Log.e("高德轨迹服务--开始采集轨迹回调", msg);
            }
        }

        @Override
        public void onStopGatherCallback(int i, String s) {

        }

        @Override
        public void onStopTrackCallback(int i, String s) {

        }
    };

    public AMapTrackService(Context ctx, ITrackListener listener) {
        context = ctx;
        this.listener = listener;
        try {
            client = new AMapTrackClient(context);
            client.setInterval(1, 10);
            client.setOnTrackListener(callback);
        } catch (Exception e) {
            Log.e("高德轨迹服务--创建轨迹实例", Objects.requireNonNull(e.getMessage()));
        }
    }

    public void Start() {
        client.queryTerminal(new QueryTerminalRequest(SERVICE_ID, Utils.GetDeviceName()), this);
    }

    public void Stop() {
        client.stopGather(callback);
    }

    private void StartTrack(long tid) {
        client.startTrack(new TrackParam(SERVICE_ID, tid), callback);
    }

    @Override
    public void onQueryTerminalCallback(QueryTerminalResponse queryTerminalResponse) {
        if (!queryTerminalResponse.isSuccess()) {
            Log.e("高德轨迹服务--查询终端回调", queryTerminalResponse.getErrorMsg());
            return;
        }
        long tid = queryTerminalResponse.getTid();
        if (tid > 0) {
            StartTrack(tid);
            return;
        }
        client.addTerminal(new AddTerminalRequest(Utils.GetDeviceName(), SERVICE_ID), this);
    }

    @Override
    public void onCreateTerminalCallback(AddTerminalResponse addTerminalResponse) {
        if (!addTerminalResponse.isSuccess()) {
            Log.e("高德轨迹服务--创建终端回调", addTerminalResponse.getErrorMsg());
            return;
        }
        long tid = addTerminalResponse.getTid();
        StartTrack(tid);
    }

    @Override
    public void onDistanceCallback(DistanceResponse distanceResponse) {

    }

    @Override
    public void onLatestPointCallback(LatestPointResponse latestPointResponse) {
        Log.i("高德轨迹服务--最后一个点回调", latestPointResponse.getLatestPoint().getPoint().getLng() + " " + latestPointResponse.getLatestPoint().getPoint().getLat());
    }

    @Override
    public void onHistoryTrackCallback(HistoryTrackResponse historyTrackResponse) {

    }

    @Override
    public void onQueryTrackCallback(QueryTrackResponse queryTrackResponse) {

    }

    @Override
    public void onAddTrackCallback(AddTrackResponse addTrackResponse) {
        Log.i("高德轨迹服务--添加轨迹回调", addTrackResponse.getTrid() + "");
    }

    @Override
    public void onParamErrorCallback(ParamErrorResponse paramErrorResponse) {

    }
}
